package com.fyzk.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import java.util.Timer;
import java.util.TimerTask;

public class RealTimeScoreWebViewActivity extends BaseActivity implements
        onTopTitleClickListener {

    private String title = "";
    private String webId = "";
    private int type;
    private WebView webView;

    private TopTitleView topView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_info_detail_webview);
        getInfo();

    }

    public static void actionStart(Context context, String title, String url, int type) {
        if (context == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(context, RealTimeScoreWebViewActivity.class);
        intent.putExtra(IntentParamUtils.TITLE, title);
        intent.putExtra(IntentParamUtils.URL, url);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    private void getInfo() {
        Intent intent = getIntent();
        if (intent != null) {
            title = intent.getStringExtra(IntentParamUtils.TITLE);
            webId = intent.getStringExtra(IntentParamUtils.URL);
            type = intent.getIntExtra("type", 0);
        }
        initView(title, webId);
    }

    private void initView(String title, String webId) {
        topView = (TopTitleView) findViewById(R.id.topTitle);
        topView.setRightVisible(View.GONE);
        topView.setCenterText(title);
        topView.setOnTitleClickListener(this);
        initWebView(webId);
    }

    public void initWebView(String webId) {
        webView = (WebView) findViewById(R.id.ruyipackage_webview);
        String url = webId;
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        settings.setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        settings.setSupportZoom(true);//是否可以缩放，默认true
        settings.setBuiltInZoomControls(true);//是否显示缩放按钮，默认false
        settings.setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        settings.setAppCacheEnabled(false);//是否使用缓存
        settings.setDomStorageEnabled(true);//DOM Storage
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
        webView.setWebViewClient(new NoAdWebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                Log.e("fjl", "onProgress>" + view.getUrl());
                super.onProgressChanged(view, newProgress);
                boolean back = view.getUrl().contains("match-detail/ft/") && !view.getUrl().contains("?");
                if (view.getUrl().contains("a=login") || view.getUrl().contains("situation") || back) {
                    view.goBack();
                }
                evaluateJavascript(view);
                stopTimer();
                startTimer(view);
            }

        });
        startTimer(webView);
    }

    int number = 10000;//设置运行五次


    private Timer timer = null;//计时器
    private TimerTask task = null;

    private void startTimer(WebView view) {
        timer = new Timer();
        task = new TimerTask() {
            int count = 0;    //从0开始计数，每运行一次timertask次数加一，运行制定次数后结束。

            @Override
            public void run() {
                if (count < number) {
                    evaluateJavascript(view);
                } else {
                    timer.cancel();
                }
                count++;
            }
        };
        //100ms执行一次
        timer.schedule(task, 100, 100);
    }

    private void stopTimer() {
        if (timer != null)
            timer.cancel();
        timer = null;//据说这样不会自动跳出软件
    }

    @Override
    public void finish() {
        super.finish();
        stopTimer();
    }

    private boolean canJump = true;

    @Override
    public void onResume() {
        super.onResume();
        canJump = true;
    }

    public void evaluateJavascript(WebView view) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (view == null) return;
                view.evaluateJavascript("document.documentElement.getElementsByClassName('detailTop topBarMove2')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('floatLayer-container')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('refresh-box')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('switch-box')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('navigator')[0].getElementsByTagName('li')[3].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('sk-btips')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('gl-box box-bsyp')[0].style.display = 'none'", null);
                if (type == 0) {
                    view.evaluateJavascript("document.documentElement.getElementsByClassName('navigator')[0].getElementsByTagName('li')[5].style.display = 'none'", null);
                }
            }
        });

    }

    public void evaluateDetailJavascript(WebView view) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (view == null) return;
                view.evaluateJavascript("document.documentElement.getElementsByClassName('detailTop topBarMove2')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('floatLayer-container')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('refresh-box')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('switch-box')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('gl-nav')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('articlelist-wrap')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('navigator')[0].getElementsByTagName('li')[3].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('sk-btips')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('gl-box box-bsyp')[0].style.display = 'none'", null);
                view.evaluateJavascript("document.documentElement.getElementsByClassName('ui-empty-other')[0].style.display = 'none'", null);
                if (type == 0) {
                    view.evaluateJavascript("document.documentElement.getElementsByClassName('navigator')[0].getElementsByTagName('li')[5].style.display = 'none'", null);
                }
            }
        });

    }

    public void onPause() {
        super.onPause();
    }

    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }

    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        finish();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }

    public class NoAdWebViewClient extends WebViewClient {
        public NoAdWebViewClient() {
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (view != null) {
                        evaluateDetailJavascript(view);
                    }
                }
            }, 200);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            evaluateJavascript(view);
        }
    }
}
